<template>
  <el-card class="box-card">

    <!-- <span class="ivu-alert-message"><h3 data-v-668e4ae7="">【重要通知】：</h3></span> -->
    <el-form ref="form"
             :model="form"
             inline>

      <el-form-item prop="search">
        <el-input v-model="form.search"
                  clearable
                  style="width:300px"
                  prefix-icon="search"
                  placeholder="输入名称" />
      </el-form-item>

      <el-form-item>
        <el-button type="success"
                   icon="search"
                   size="small"
                   @click="search(form)">搜索</el-button>
        <el-button type="warning"
                   icon="refresh-left"
                   size="small"
                   @click="resetForm()">重置</el-button>
      </el-form-item>

    </el-form>

    <el-button 
               type="primary"
               style="margin-bottom:20px"
               icon="plus"
               size="small"
               @click="createssh()">新增</el-button>


    <!--表单组件-->
    <el-table ref="table"
              :data="tableData"
              style="width: 100%"
              row-key="id"
              highlight-current-row>

      <el-table-column prop="name"
                       label="名称"
                       min-width="100"
                       show-overflow-tooltip />

      <el-table-column prop="ssh_user"
                       label="账号"
                       min-width="100"
                       show-overflow-tooltip />
      <el-table-column prop="ssh_port"
                       label="端口"
                       min-width="100"
                       show-overflow-tooltip />

      <el-table-column prop="ssh_type_display"
                       label="类型"
                       min-width="100"
                       show-overflow-tooltip
                       />
    
      

      <el-table-column prop="desc"
                       label="备注"
                       min-width="100"
                       show-overflow-tooltip />
                       

      <el-table-column prop="gmt_create"
                       label="创建时间" />

      <el-table-column fixed="right"
                       align="center"
                       label="操作"
                       width="220">
        <template  #default="scope">
          <el-button 
                     type="primary"
                     icon="edit"
                     size="small"
                     @click="updatessh(scope.row)">编辑</el-button>
          <el-button 
                     type="danger"
                     icon="delete"
                     size="small"
                     @click="deletessh(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <el-pagination :current-page="1"
                   :page-sizes="[10, 20, 50, 100]"
                   :page-size="10"
                   :total="total"
                   layout="total, sizes, prev, pager, next, jumper"
                   @size-change="handleSizeChange"
                   @current-change="handleCurrentChange" />

    <cuForm :dialog-visible="cuDialogVisible"
            :cur-id="curId"
            @close="close"
            @search="search" />

  </el-card>
</template>
<script>
import cuForm from './components/cuForm'
import {
  deletessh,
  getsshs,
} from '@/api/asset/ssh'

export default {
  name: 'sshKey',
  components: { cuForm },
  data () {
    return {
      form: {
        page: 1,
        size: 10,
        search: '',
        ordering: ''
      },
      tableData: [],
      total: 0,
      isAllSelect: false,
      multipleSelection: [],
      showpass:false,  //明密文全局变量
      // cuForm数据
      cuDialogVisible: false,
      curId: null
      
    }
  },
  created () {
    this.search()
  },
  methods: {
    // 获取列表/搜索功能
    search () {
      getsshs(this.form).then(res => {
        this.tableData = res.data.results;
        this.total = res.data.count;
      })
    },
    // 重置
    resetForm () {
      this.$refs.form.resetFields()
      this.search()
    },
    // 删除
    deletessh (row) {
      this.$confirm('此操作将删除"' + row.name + '" , 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletessh(row.id).then(res => {
            res.data
          this.$message({
            message: '删除' + row.name + '成功',
            type: 'success'
          })
          // 刷新table
          this.search()
        })
      })
    },


    // cuForm子组件
    createssh () {
      this.cuDialogVisible = true
    },
    updatessh (row) {
      this.curId = row.id
      this.cuDialogVisible = true
    },
    close () {
      this.cuDialogVisible = false
      this.curId = null
    },
    // 分页
    handleSizeChange (val) {
      this.form.size = val;
      this.search();
    },
    handleCurrentChange (val) {
      this.form.page = val;
      this.search();
    },

  }
}
</script>
